'''
@author: pnuzhnyi
'''

from events.repository import events_repository
from surfers.repository import surfer_repository
import random
import logging

def participate(event,surfer):
    """
        Signs surfer up for the event
    """
    surferStats = surfer.stats
    
    if surfer.fbook_id in event.participants: return False
    if surferStats["cash"] < event.fee: return False
    
    surferStats["cash"] = surferStats["cash"] - event.fee 
    surfer.stats = surferStats
    
    surfer_repository().update_surfer(surfer)
    events_repository().add_event_participant(event.id, surfer.fbook_id)
    
    return True

def run_event_simulation(event):
    logging.error("running simulation for event %s(%s)" % (event.name,event.id))
    
    #make sure the event status is still 'pending'
    if event.status != 'pending':
        logging.error("Cannot run simulation for this event, the status is: %s" % event.status)
        return
    
    event_repo = events_repository()
    surfer_repo = surfer_repository()
    
    #randomly choose winners
    participants = event.participants
    random.shuffle(participants)
    
    logging.error("results: %s" % participants)
    
    #save winners in the base
    i=1
    for participant in participants:
        logging.error("result for %s: %s" % (participant,i))
        event_repo.add_event_winner(event.id, participant, i)
        i = i+1
        
    #mark event as finished
    event.status = "over"
    event_repo.update_event(event)
    
    #update winner stats
    logging.error("updating winner stats")
    event_winners = event_repo.get_winners_for_event(event.id)
    
    for ew in event_winners:
        if not ew.updated:
            logging.error("updating %s place=%s"%(ew.participant_id,ew.place))
            compensation = calculate_compensation(ew.place, event)
            
            ew.compensation = compensation
            ew.update = True
        
            event_repo.update_event_winner(ew)
            
            surfer = surfer_repo.get_or_create_surfer(ew.participant_id)
            if surfer is not None:
                surfer_repo.boost_stats(surfer, compensation)
            
        else: logging.error("%s is already updated" % ew.participant_id)

def calculate_compensation(place,event):
    logging.error("computing compensation: place = %s" % place)
    money = 0
    experience = 0
    strength = 0
    stamina = 0
    
    if place > 3: 
        logging.error("place > 3, no money, no experience. +stamina +strength")
        stamina = 1
        strength = 1
    else:
        experience = (4-place)*10
        # 1st = 50% 2nd = 30% 3rd = 20%
        money = 0
        if place == 1: money = event.prize * 0.5
        if place == 2: money = event.prize * 0.3
        if place == 3: money = event.prize * 0.2
        
        stamina = (4-place)
        strength = (4-place)
    
    boost_package = {"experience":experience, "strength": strength, "stamina": stamina,"cash": money}
    
    logging.error("resulting compensation: %s" % boost_package)
    
    return boost_package   

def generate_event_result_notification(user_id):
    e_repo = events_repository()
    wins = e_repo.get_winners_by_user(user_id)
    
    if len(wins) == 0: return None
    
    result = []
    for win in wins: 
        if win.notified: continue
        e = e_repo.get_event_details(int(win.event_id))
        result.append({"event":e,"win":win})
    
    return result
    
     
    
            